package com.freewater.springboot.demo.spel.parseconfig;

import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelExpressionParser;

import java.util.List;

public class Main {
    public static void main(String[] args) {
        class Demo {
            public List<String> list;
        }
        // Turn on:
        // - auto null reference initialization
        // - auto collection growing
        SpelParserConfiguration configuration = new SpelParserConfiguration(true, true);
        ExpressionParser parser = new SpelExpressionParser(configuration);
        final Expression expression = parser.parseExpression("list[3]");
        //
        Demo demo = new Demo();
        final Object value = expression.getValue(demo);
        // demo.list will now be a real collection of 4 entries
        // Each entry is a new empty String
        System.out.println(demo.list);
        System.out.println(demo.list.size());
    }
}
